//
//  ConfirmViewController.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/18.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "ConfirmDeleteViewController.h"
#import "AlbumLogic.h"

@interface ConfirmDeleteViewController ()

@end

@implementation ConfirmDeleteViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupBackground];
    self.view.tintColor = SAColor(255, 30, 29);
    self.circleBtnTitleColor = [UIColor whiteColor];
    
    // 设置标题文本
    self.titleLable.text = NSLocalizedString(@"EnterCurrentPassword", nil);
    self.titleLable.textColor = [UIColor whiteColor];

    // 设置Cancel按钮
    [self.actionBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    [self.actionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.actionBtn addTarget:self action:@selector(cancelBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 私有方法

- (void)cancelBtnOnclick:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupBackground
{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[self blur:self.backgroundImgFromBeforeView]];
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
    backgroundView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.6f];
    [self.view addSubview:backgroundView];
    [self.view sendSubviewToBack:backgroundView];
}

- (UIImage*)blur:(UIImage*)theImage
{
    // create our blurred image
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:theImage.CGImage];
    
    CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0);
    [affineClampFilter setValue:inputImage forKey:kCIInputImageKey];
    [affineClampFilter setValue:[NSValue valueWithBytes:&xform
                                               objCType:@encode(CGAffineTransform)]
                         forKey:@"inputTransform"];
    
    CIImage *extendedImage = [affineClampFilter valueForKey:kCIOutputImageKey];
    
    // setting up Gaussian Blur (could use one of many filters offered by Core Image)
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setValue:extendedImage forKey:kCIInputImageKey];
    [blurFilter setValue:[NSNumber numberWithFloat:18.0f] forKey:@"inputRadius"];
    CIImage *result = [blurFilter valueForKey:kCIOutputImageKey];
    
    // CIGaussianBlur has a tendency to shrink the image a little,
    // this ensures it matches up exactly to the bounds of our original image
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
    
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];
    //create a UIImage for this function to "return" so that ARC can manage the memory of the blur...
    //ARC can't manage CGImageRefs so we need to release it before this function "returns" and ends.
    CGImageRelease(cgImage);//release CGImageRef because ARC doesn't manage this on its own.
    
    return returnImage;
}

#pragma mark - DotsViewDelegate

- (BOOL)verifyPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    if ([[AlbumLogic currentAlbumID] isEqualToString:password]) {
        return YES;
    } else {
        self.titleLable.text = NSLocalizedString(@"PasswordError", nil);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.titleLable.text = NSLocalizedString(@"EnterCurrentPassword", nil);
        });
        
        return NO;
    }
}

- (void)successPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(successPassword:confirmDeleteViewController:)]) {
        [self.delegate successPassword:password confirmDeleteViewController:self];
    }
}

@end
